package gov.ccswj.modules.app.controller;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.Key;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

//import org.junit.Test;
import org.xidea.el.json.JSONDecoder;
import org.xidea.el.json.JSONEncoder;

import com.quantum.auth.RSA2048;

import gov.ccswj.common.utils.AuthUtils;
import gov.ccswj.common.utils.HttpRequest;

public class AuthTest {
	
	public static void main(String[] args) {
		try {
			  String uid = "bcc9a8b6f5ea4047b3064da563c47dbd"; //用户id
		      String serviceUrl = "http://121.52.214.35/service";
		      String serverName = "getEntByKeyword"; //服务方法名称
		      String serverParams = getServerJsonStr(uid, serverName);
		      String postParams = MessageFormat.format("uid={0}&data={1}", uid, getPostJsonStr(serverParams,uid));
		      //获取数据   String postParams = MessageF
		      String result = HttpRequest.sendPost(serviceUrl, postParams);
		      result = URLDecoder.decode(result, "UTF-8");
		      //转换数据
		      Map<String, Object> resultMap = JSONDecoder.decode(result);
		      String value = RasAndDesDecode(resultMap,uid);
	          System.out.println(value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
//    @Test
//    public void Auth() throws Exception {
//        //加密过程
//        String uid = ""; //用户id
//        String serviceUrl = "htp://[ip或域名]/service";
//        String serverName = "getEntByKeyword"; //服务方法名称
//        String serverParams = getServerJsonStr(uid, serverName);
//        String postParams = MessageFormat.format("uid={0}&data={1}", uid, getPostJsonStr(serverParams));
//        //获取数据   String postParams = MessageF
//        String result = HttpRequest.sendPost(serviceUrl, postParams);
//        result = URLDecoder.decode(result, "UTF-8");
//        //转换数据
//        Map<String, Object> resultMap = JSONDecoder.decode(result);
//        //解密数据
//        try {
//            String value = RasAndDesDecode(resultMap);
//            System.out.println(value);
//        } catch (Exception ex) {
//            //TODO 解密出错，说明返回数据未加密
//        }
//    }

    /**
     * 解密过程
     *
     * @param data
     * @return
     * @throws Exception
     */
    private static String RasAndDesDecode(Map<String, Object> data,String uid) throws Exception {
        return AuthUtils.decode(data, getKey(uid));
    }

    /**
     * 获取公钥
     *
     * @return
     * @throws Exception
     */
    private static Key getKey(String uid) throws Exception {
        Key key = null;
        try {
        	URL url = AuthTest.class.getResource("/");
            key = RSA2048.getKey(url.getPath()+"public_"+uid+".key");//密钥路径
        } catch (Exception ex) {
            throw ex;
        }
        return key;
    }

    /**
     * 服务参数加密
     *
     * @param serverParams
     * @return 返回加密后的密文
     * @throws Exception
     */
    private  static String getPostJsonStr(String serverParams,String uid) throws Exception {
        Map<String, Object> authParams = AuthUtils.encode(serverParams, getKey(uid));
        return URLEncoder.encode(JSONEncoder.encode(authParams), "UTF-8");
    }

    /**
     * 服务参数
     *
     * @param uid        用户Uid
     * @param serverName 服务方法名称
     * @return 服务相关参数的JSON数据
     */
    private static String getServerJsonStr(String uid, String serverName) {
        Map<String, Object> conditoinMap = new HashMap<String, Object>();
        conditoinMap.put("uid", uid);
        conditoinMap.put("service", serverName);
        conditoinMap.put("page", 2);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("keyword", "长春");
        params.put("type", "0");
        params.put("page", 2);
        conditoinMap.put("params", params);

        return JSONEncoder.encode(conditoinMap);
    }
}
